package com.hermes.action.login;

import java.io.IOException;

import org.apache.log4j.Logger;

import com.hermes.biz.common.model.user.UserDO;
import com.hermes.biz.service.user.UserService;
import com.hermes.common.action.BaseAction;
import com.hermes.common.util.ParameterParserUtil;
import com.hermes.utils.logger.LoggerUtil;

public class RegisterAction extends BaseAction {

    private static final long serialVersionUID = -2841153430630242279L;

    private static Logger     log              = Logger.getLogger(LoginAction.class);

    private UserService       userService;

    public String             name;

    public String execute() throws IOException {

        //        String username = ParameterParserUtil.getParameter("nick");
        //        String password = ParameterParserUtil.getParameter("password");
        //        String email = ParameterParserUtil.getParameter("email");

        String username = ParameterParserUtil.getParameter("UserName");
        String password = ParameterParserUtil.getParameter("UserPwd");
        String email = ParameterParserUtil.getParameter("email");

        LoggerUtil.error(log, password, username, password);

        name = username;

        session.put("userName", name);

        UserDO userDO = new UserDO();
        long userId = genId(email);
        session.put("userId", userId);
        userDO.setUserId(userId);
        userDO.setEmailAddress(email);
        userDO.setUserName(username);
        userDO.setUserPassword(password);

        userDO.setRegistIp(ParameterParserUtil.getUserIpAddress());

        boolean rstCod = this.userService.registerNewUserAccount(userDO);

        if (!rstCod) {
            log.error("register-fail!");
        }

        return SUCCESS;
    }

    private long genId(String name) {

        long result = 0;

        for (int i = 0; i < name.length(); i++) {
            int m = (int) name.charAt(i);
            for (int j = i; j < name.length() - 1; j++) {
                m *= 100;
            }
            result += m;
        }

        if (result < 0) {
            result = 0 - result;
        }

        return result;

    }

    public void receiveFile() {

    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
